<template>
  <ul class="aui-list aui-media-list promotion_list" style="min-height: 14rem;">
    <commodity-promotion v-for="(item,index) in goodsList"
                         :key="index"
                         :comInfor="translateDetailData(item)" :goodsQuantity="queryGoodsQuantity (item)"
                         ref="items">

    </commodity-promotion>
  </ul>
</template>

<script>
  import {queryGoodsListNormal} from '@/utils/ReturnAxios'
  import CommodityPromotion from "@/real-components/CommodityPromotion"
  import {translateDetailData} from '@/utils/TranslateDetailDate'

  export default {
    data () {
      return {
        goodsList: [],
        storeCode: localStorage.getItem('storeCode'),
        workingInfo: '',
        weightType: 7,
      }
    },
    props: {
      regularInfo: {
        type: Array,
        default: () => []
      }
    },
    created () {

      this.queryInfo()
    },
    mounted () {
      this.$emit('changeAdequateStock', this.adequateStock)
    },
    components: {
      CommodityPromotion
    },
    methods: {
      //数据转化
      translateDetailData,
      queryGoodsQuantity (data) {
        var goodsQuantity = 1
        var numberOB = this.regularInfo.find((item) => {
          return item.goodsCode == data.goodscode
        })
        if (numberOB) {
          goodsQuantity = numberOB.goodsQuantity
        }
        return goodsQuantity
      },
      getCodeList(){
        var codeList = this.regularInfo.map(
          (item) => {
            return item.goodsCode
          }
        )
        return codeList
      },
      async queryInfo () {
        var res
        var codeList = this.getCodeList()
        try {
          res = await queryGoodsListNormal({goodscodeList: codeList})
          res = res.data
        } catch (erro) {
        }
        this.goodsList = res.data[0]
      },


      addToCart (){
        var sessionId = localStorage.getItem('sessionId')
        sessionId = sessionId == "undefind" ? '' : sessionId
        var config = this.goodsList.map((data) => {
          var cartDate = this.findCartList(data.goodscode)
          var goodsNum = this.queryGoodsQuantity(data)
          var workingInfo = cartDate.workingInfo || ''
          var goodsType = data.goodstype || 0
          var minsaleqty = data.minsaleqty || 1
          var ladderqty = data.ladderqty || 1
          var storeCode = localStorage.getItem('storeCode')
          var addPrice = data.wsprice
          if (goodsType == this.weightType) {
            var restItem = this.cartList.find((item) => {
              return data.goodscode == item.goodsCode
            })
            if (restItem && restItem.goodsNum != 0) {
              goodsNum = Math.ceil(goodsNum / parseFloat(ladderqty))
            } else {
              var listNum = parseFloat(goodsNum) - parseFloat(minsaleqty)
              goodsNum = listNum >= 0 ? Math.ceil(listNum / parseFloat(ladderqty)) + 1 : 1
            }
          }
          return {
            goodsId: data.goodsid,
            goodsCode: data.goodscode,
            goodsType: data.goodstype,
            storeCode: storeCode,
            goodsUnit: data.goodsunit,
            goodsNum: goodsNum,
            serve: '',
            goodsWeight: -1,
            processingList: [],
            goodsDeliveryType: data.distributiontype,
            addPrice
          }
        })
        console.log(config)
        if (!sessionId) {
          config.map((item) => {
            item.preorder = true
          })
          this.$store.commit('cjy/ADD_CART_FROM_LOCAL', config)
        } else {
          this.$store.dispatch('cjy/addShoppingCart', config)
        }
        this.$store.dispatch('cjy/addCartAnimate')
      },

      findCartList  (goodsCode) {
        var cartList = this.cartList
        if (cartList) {
          var retrunLsit = cartList.find((item) => {
            return item.goodsCode == goodsCode
          })
          return retrunLsit || {}
        }
        return {}
      }
    },
    computed: {
      totalPrice () {
        var goodsList = this.goodsList
        var totalPrie = 0
        goodsList.map((item) => {
          var goodsCode = item.goodscode
          var price = item.wsprice || item.wsprice
          var numberOB = this.regularInfo.find((item) => {
            return item.goodsCode == goodsCode
          })
          var number = numberOB ? numberOB.goodsQuantity : 1
          // if (item.goodstype == 6) {
          //   var weight = item.weighingbig  //称重最大重量
          //   console.log(weight)
          //   totalPrie = totalPrie + item.wsprice * number * weight
          // } else {
            totalPrie = totalPrie + item.wsprice * number
          // }
        })
        return totalPrie
      },

      stockThreshold() {
        var getConfigParams = this.getConfigParams || {}
        return getConfigParams.stockThreshold || 0
      },
      // 是否所有的 商品的库存都充足
      adequateStock () {
        var goodsList = this.goodsList //商品详情列表
        var stockThreshold = this.stockThreshold // 商品库存阈值
        var cartLit = this.cartList // 购物车列表
        var adequateStock = false
        if (goodsList) {
          // 判断是否所有库存都充足
          var adequateStock = goodsList.every((list) => {
            var stockCount = list.stockcount  // 商品库存
            var goodsType = list.goodstype  // 商品类型
            //查找购物车中对应商品的信息
            var goodsNumObj = cartLit.find((item) => {
                return list.goodscode == item.goodsCode
              }
            )
            var goodsNum = goodsNumObj ? goodsNumObj.goodsNum + 1 : 1 // 假设如果要增加一件到购物车后的 数量
            var minsaleQty = list.minsaleqty // 最小购买数量
            var ladderQty = list.ladderqty  // 购买阶梯数量
            var weighingBig = list.weighingbig
            if (goodsType == this.weightType) {
              goodsNum = minsaleQty + (goodsNum - 1) * ladderQty // 购买的重量
            }
            return stockCount - stockThreshold > goodsNum * weighingBig//是否超出库存
          })
        }
        return adequateStock // 如果库存充足就返回true
      },

      cartList () {
        return this.$store.state.cjy.activeCartList
      }
    },
    watch: {
      totalPrice (data) {
        //将
        this.$emit('changeTotalPrice', data)
      },
      adequateStock (data) {
        //可购买状态返回给父组件
        this.$emit('changeAdequateStock', data)
      }
    },
    updated () {
      this.$nextTick(
        () => {
          this.$emit('refresh')
        }
      )
    }


  }
</script>

<style>

</style>
